<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最大子数组和</title>
</head>
<body>
  <script>
    function fn(nums) {
      let ans = 0, max = nums[0]
      nums.forEach((x) => {
        ans = Math.max(ans + x, x)
        max = Math.max(max, ans)
      });
      return max
    }
    console.log(fn([-2, 1, -3, 4, -1, 2, 1, -5, 4]))
  </script>
</body>
</html>